<?php


namespace app\zftpay\controller;


use app\BaseController;
use app\service\propertyService;
use app\service\zftService;
use app\zftpay\service\apiService;
use think\facade\Db;
use think\facade\Log;

class Pay extends BaseController
{

    public function trade(){
        session('user_id',"2088112618452297");
        $community_id="AFXGQANS83301";
        $money=request()->post("money");
        //$gateinfo=Db::table("property_parking_gate")->where("id",$gateid)->find();
        $propertyZftInfo=(new propertyService())->propertyZftByCommunityId($community_id);
        if(empty($propertyZftInfo)){
            return json(['info' => '直付通配置为空']);
        }

        $info["property_id"]=1;


        $info["order_money"]=request()->post("money");
        $info["remark"]=request()->post("remark");
        $info["user_id"]=session("user_id");
        //$info["user_id"]="2088222524772492";
        $info["community_name"]="早早测试";
        $info["pay_money"]=request()->post("money");
        $info["out_trade_no"]=date('YmdHiss').rand(100000,999999);
        $info["subject"] = "测试支付";//$gateinfo["community_name"]."-".$gateinfo["gateName"];

        $propertyinfo= Db::name('property')->where('id',$propertyZftInfo["property_id"])->find();

        $api = new zftService();

        $notifyUrl="https://www.zaozaopay.com/zftpay/callback/notice";

        $settle_detail_info=array(
            'trans_in_type'=>'defaultSettle',//'loginName',
            'trans_in'=>'',//收款账号aliwykd@163.com
            'amount'=>$info["pay_money"],//结算金额
        );
        $data= array(
            'out_trade_no'=> $info["out_trade_no"],
            'total_amount'=>$info["pay_money"],
            'buyer_id'=>$info["user_id"],//$info["user_id"],
            'subject' =>$info["subject"],
            'notifyurl'=>$notifyUrl,
            'merchant_id'=>$propertyZftInfo["merchant_id"],
            'notifyUrl'=>$notifyUrl,
            'settle_detail_info'=>$settle_detail_info,
        );
        $result = $api->create(json_encode($data));

        Log::write($result);
        if(property_exists($result,'sub_msg')){
            return json(['info' => $result->sub_msg]);
        }else{

            $info['trade_no'] =  $result->trade_no;
            $res=Db::table("zft_order")->insert($info);
            //返回结果到前台唤起收银台
            if($res){
                $data["trade_no"]=$info["trade_no"];
                parent::sendOutput($data);
                //return json(['trade_no' => $info['trade_no']]);
            }else{
                return json(['info' => '订单存储失败',"code"=>500]);
            }
        }
    }

}